#import "PeerInfoWindowController.h"
#import "SolarSeekApplication.h"
#import "PeersController.h"

NSMutableDictionary *windowsOpen = nil;

@interface PeerInfoWindowController (Private)
- (id)initWithPeer:(Peer *)peer;
@end

@implementation PeerInfoWindowController

+ (void)initialize {
	if(windowsOpen ==nil)
		windowsOpen = [[NSMutableDictionary alloc] init];
}

- (IBAction)toggleBan:(id)sender
{
}

- (IBAction)toggleComment:(id)sender
{
}

- (IBAction)toggleFriend:(id)sender
{
}

- (IBAction)toggleGeneral:(id)sender
{
}

- (IBAction)toggleIgnore:(id)sender
{
}

+ (void)showInfoForPeer:(Peer *)peer {	
	PeerInfoWindowController *piwc = [windowsOpen objectForKey:[peer name]];
	if(piwc == nil) {
		@synchronized(self) {
			if(piwc==nil) {
				[[[NSApp engine] peersController] refreshInformationForPeer:peer];
				piwc = [[PeerInfoWindowController alloc] initWithPeer:peer];
				[windowsOpen setObject:piwc forKey:[peer name]];
				[piwc release];
			}
		}
	}
	[[piwc window] makeKeyAndOrderFront:self];
}

- (id)initWithPeer:(Peer *)p {
	if(self = [super initWithWindowNibName:@"PeerInfoWindow"]) {
		peer = [p retain];
	}
	return self;
}

- (void)dealloc {
	[peer release];
	[super dealloc];
}

- (void)windowWillClose:(NSNotification *)note {
//	if([self window] == [note object]) {
//		[windowsOpen removeObjectForKey:[[nibControllerPeer content] name]];
//	}
}

- (void)windowDidBecomeKey:(NSNotification *)note {
	NSString *notee = [[self peer] note];
	if(notee)
		[nibTextNote setString:[[self peer] note]];
}

- (void)updateNote {
	[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(updateNote) object:nil];
	[[self peer] setNote:[nibTextNote string]];
}

- (void)textDidChange:(NSNotification *)note {
	[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(updateNote) object:nil];
	[self performSelector:@selector(updateNote) withObject:nil afterDelay:3.0];
}

- (void)textDidEndEditing:(NSNotification *)note {
	[self updateNote];
}

- (void)awakeFromNib {
	[nibControllerPeer setContent:peer];
}

- (Peer *)peer { return peer; }

@end
